﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MemDrawMoney
    {
        public int Add(Chain.Model.MemDrawMoney model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemDrawMoney(");
            builder.Append("DrawMoneyMemID,DrawMoneyAccount,DrawMoney,DrawActualMoney,DrawMoneyRemark,DrawMoneyShopID,DrawMoneyUserID,DrawMoneyCreateTime)");
            builder.Append(" values (");
            builder.Append("@DrawMoneyMemID,@DrawMoneyAccount,@DrawMoney,@DrawActualMoney,@DrawMoneyRemark,@DrawMoneyShopID,@DrawMoneyUserID,@DrawMoneyCreateTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@DrawMoneyMemID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyAccount", SqlDbType.VarChar, 50), new SqlParameter("@DrawMoney", SqlDbType.Money, 8), new SqlParameter("@DrawActualMoney", SqlDbType.Money, 8), new SqlParameter("@DrawMoneyRemark", SqlDbType.NVarChar, 500), new SqlParameter("@DrawMoneyShopID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyUserID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.DrawMoneyMemID;
            cmdParms[1].Value = model.DrawMoneyAccount;
            cmdParms[2].Value = model.DrawMoney;
            cmdParms[3].Value = model.DrawActualMoney;
            cmdParms[4].Value = model.DrawMoneyRemark;
            cmdParms[5].Value = model.DrawMoneyShopID;
            cmdParms[6].Value = model.DrawMoneyUserID;
            cmdParms[7].Value = model.DrawMoneyCreateTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int DrawMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemDrawMoney ");
            builder.Append(" where DrawMoneyID=@DrawMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@DrawMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = DrawMoneyID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string DrawMoneyIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemDrawMoney ");
            builder.Append(" where DrawMoneyID in (" + DrawMoneyIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int DrawMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemDrawMoney");
            builder.Append(" where DrawMoneyID=@DrawMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@DrawMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = DrawMoneyID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetDataByTime(DateTime starttime, DateTime endtime, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthDrawMoney", parameters, "#DrawMoneyData");
        }

        public decimal GetDrawMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select sum(DrawActualMoney) from MemDrawMoney,Mem,MemLevel");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public DataSet GetDrawMoneyCount(string strSql)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select isnull(sum(DrawMoney),0) as DrawMoney,isnull(sum(DrawActualMoney),0) as DrawActualMoney from MemDrawMoney,Mem ");
            if (strSql.Trim() != "")
            {
                builder.Append(" where " + strSql);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DrawMoneyID,DrawMoneyMemID,DrawMoneyAccount,DrawMoney,DrawActualMoney,DrawMoneyRemark,DrawMoneyShopID,DrawMoneyUserID,DrawMoneyCreateTime ");
            builder.Append(" FROM MemDrawMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" DrawMoneyID,DrawMoneyMemID,DrawMoneyAccount,DrawMoney,DrawActualMoney,DrawMoneyRemark,DrawMoneyShopID,DrawMoneyUserID,DrawMoneyCreateTime ");
            builder.Append(" FROM MemDrawMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.DrawMoneyID desc");
            }
            builder.Append(")AS Row, T.*  from MemDrawMoney T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "MemDrawMoney,Mem,MemLevel,SysShop,SysUser";
            string[] column = new string[] { "MemDrawMoney.*,Mem.*,MemLevel.LevelName,SysShop.ShopName,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "DrawMoneyID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("DrawMoneyID", "MemDrawMoney");
        }

        public Chain.Model.MemDrawMoney GetModel(int DrawMoneyID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 DrawMoneyID,DrawMoneyMemID,DrawMoneyAccount,DrawMoney,DrawActualMoney,DrawMoneyRemark,DrawMoneyShopID,DrawMoneyUserID,DrawMoneyCreateTime from MemDrawMoney ");
            builder.Append(" where DrawMoneyID=@DrawMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@DrawMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = DrawMoneyID;
            Chain.Model.MemDrawMoney money = new Chain.Model.MemDrawMoney();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyID"] != null) && (set.Tables[0].Rows[0]["DrawMoneyID"].ToString() != ""))
            {
                money.DrawMoneyID = int.Parse(set.Tables[0].Rows[0]["DrawMoneyID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyMemID"] != null) && (set.Tables[0].Rows[0]["DrawMoneyMemID"].ToString() != ""))
            {
                money.DrawMoneyMemID = int.Parse(set.Tables[0].Rows[0]["DrawMoneyMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyAccount"] != null) && (set.Tables[0].Rows[0]["DrawMoneyAccount"].ToString() != ""))
            {
                money.DrawMoneyAccount = set.Tables[0].Rows[0]["DrawMoneyAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["DrawMoney"] != null) && (set.Tables[0].Rows[0]["DrawMoney"].ToString() != ""))
            {
                money.DrawMoney = decimal.Parse(set.Tables[0].Rows[0]["DrawMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawActualMoney"] != null) && (set.Tables[0].Rows[0]["DrawActualMoney"].ToString() != ""))
            {
                money.DrawActualMoney = decimal.Parse(set.Tables[0].Rows[0]["DrawActualMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyRemark"] != null) && (set.Tables[0].Rows[0]["DrawMoneyRemark"].ToString() != ""))
            {
                money.DrawMoneyRemark = set.Tables[0].Rows[0]["DrawMoneyRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyShopID"] != null) && (set.Tables[0].Rows[0]["DrawMoneyShopID"].ToString() != ""))
            {
                money.DrawMoneyShopID = int.Parse(set.Tables[0].Rows[0]["DrawMoneyShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyUserID"] != null) && (set.Tables[0].Rows[0]["DrawMoneyUserID"].ToString() != ""))
            {
                money.DrawMoneyUserID = int.Parse(set.Tables[0].Rows[0]["DrawMoneyUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["DrawMoneyCreateTime"] != null) && (set.Tables[0].Rows[0]["DrawMoneyCreateTime"].ToString() != ""))
            {
                money.DrawMoneyCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["DrawMoneyCreateTime"].ToString());
            }
            return money;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemDrawMoney ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MemDrawMoney model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemDrawMoney set ");
            builder.Append("DrawMoneyMemID=@DrawMoneyMemID,");
            builder.Append("DrawMoneyAccount=@DrawMoneyAccount,");
            builder.Append("DrawMoney=@DrawMoney,");
            builder.Append("DrawActualMoney=@DrawActualMoney,");
            builder.Append("DrawMoneyRemark=@DrawMoneyRemark,");
            builder.Append("DrawMoneyShopID=@DrawMoneyShopID,");
            builder.Append("DrawMoneyUserID=@DrawMoneyUserID,");
            builder.Append("DrawMoneyCreateTime=@DrawMoneyCreateTime");
            builder.Append(" where DrawMoneyID=@DrawMoneyID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@DrawMoneyMemID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyAccount", SqlDbType.VarChar, 50), new SqlParameter("@DrawMoney", SqlDbType.Money, 8), new SqlParameter("@DrawActualMoney", SqlDbType.Money, 8), new SqlParameter("@DrawMoneyRemark", SqlDbType.NVarChar, 500), new SqlParameter("@DrawMoneyShopID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyUserID", SqlDbType.Int, 4), new SqlParameter("@DrawMoneyCreateTime", SqlDbType.DateTime), new SqlParameter("@DrawMoneyID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.DrawMoneyMemID;
            cmdParms[1].Value = model.DrawMoneyAccount;
            cmdParms[2].Value = model.DrawMoney;
            cmdParms[3].Value = model.DrawActualMoney;
            cmdParms[4].Value = model.DrawMoneyRemark;
            cmdParms[5].Value = model.DrawMoneyShopID;
            cmdParms[6].Value = model.DrawMoneyUserID;
            cmdParms[7].Value = model.DrawMoneyCreateTime;
            cmdParms[8].Value = model.DrawMoneyID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

